package com.zhl.common.factory;

import com.zhl.common.anno.IoAnno;
import com.zhl.common.enums.IoEnum;
import com.zhl.common.service.IFileService;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description 文件工厂
 * @Author: z156940l@outlook.com
 * @className IFileService
 * @Date: 2024/7/21 13:08
 */
@Component
public class FileFactory {
    public final ConcurrentHashMap<IoEnum, IFileService> HANDLE_MAP = new ConcurrentHashMap<>();

    public FileFactory(List<IFileService> fileServices) {
        if (fileServices != null && !fileServices.isEmpty()) {
            fileServices.forEach(this::setMap);
        }
    }

    public IFileService getFileByCode(IoEnum code) {
        return HANDLE_MAP.get(code);
    }

    private void setMap(IFileService iFileService) {
        IoEnum type = iFileService.getClass().getAnnotation(IoAnno.class).type();
        HANDLE_MAP.put(type, iFileService);
    }
}
